//
//  MMMVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/13.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "MMMVC.h"
#import "MMTableViewCell.h"
#import "MMMDetailVC.h"

static NSString *const IDE = @"mmvc_ide";
@interface MMMVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSource;

@property (nonatomic,assign) NSInteger currentPage;
@end

@implementation MMMVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentPage = 1;
    [self loadData];
    [self configUI];
}
- (void)loadData{
//    _currentPage = 1;
    [NetWorkingManager postWithRequestUrl:@"loadProductByType" AndParameters:@{@"parameters":[@{@"currentPage":@(_currentPage),@"type":@3} DataTOjsonString]} AndRequestBlock:^(NSDictionary *responseObject) {
        if (_currentPage == 1) {
                self.dataSource = [ProDetailModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
        }else{
                [self.dataSource addObjectsFromArray: [ProDetailModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]]];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [self reloadDataByNowState];
//        [self.tableView reloadData];

    } FailBlock:^(NSDictionary *errDic) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];

}
- (void)configUI{
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

#pragma mark Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MMMDetailVC *dVC = [[MMMDetailVC alloc]init];
    ProDetailModel *model = self.dataSource[indexPath.section];
    dVC.pId = model.ID;
    [self.navigationController pushViewController:dVC animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDE forIndexPath:indexPath];
//    [cell setCProgress:arc4random()%100*1.0/100+0.4];
    ProDetailModel *model = self.dataSource[indexPath.section];
//    productStatus;//产品状态 -3:排队中 -2：审核不通过 -1:未审核 0:营销中  1:已售罄  2:结算中   3:已还款
    if ([model.productStatus isEqualToString: @"0"]) {
           [cell setModel:model];
    }else{
            model.wcjd = @"1.0";
           [cell setModel:model];
    }
 
    
    return cell;
}
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"请确保您的网络状况是良好的";
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f]};
    
    return [[NSAttributedString alloc] initWithString:@"~点击此处重试" attributes:attributes];
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    [self loadData];
}


- (void)viewDidLayoutSubviews{
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.backgroundColor = RGBCOLOR(245, 245, 245);
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _currentPage = 1;
            [self loadData];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            _currentPage += 1;
            [self loadData];
        }];
        [_tableView registerClass:[MMTableViewCell class] forCellReuseIdentifier:IDE];
        _tableView.separatorColor = [UIColor clearColor];
    }
    return _tableView;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self reloadDataByNowState];
}
- (void)reloadDataByNowState{
    if ([UserModel isLogin]) {
        BOOL buyFreshmanProduct = [[UserModel userDic][@"buyFreshmanProduct"] boolValue];
        if (buyFreshmanProduct) {
            NSMutableArray *mArrary = [NSMutableArray new];
            for (NSInteger i= 0; i<self.dataSource.count; i++) {
                ProDetailModel *model = self.dataSource[i];
                if ([model.productType integerValue] == 1) {
                    
                }else{
                    [mArrary addObject:model];
                }
            }
            self.dataSource = [mArrary mutableCopy];
        }
    }
    
        [self.tableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
